import Vuex, { Commit } from 'vuex'
import type { requestType, analysisType } from '@/api/modules/statistics'
import {
  getSummary,
  getDataOverview,
  getMarkedRate,
  getBottomData,
  getHospitalStaffs,
} from '@/api/modules/statistics'
import type { SummaryList } from '@/views/statistics/types'
import { AxiosResponse } from 'axios'

interface State {
  summaryList: SummaryList
  overviewList: SummaryList
  markedCard: MarkedCard
  diagnosticStatistics: DiagnosticStatistics | null
  treatmentLevel: TreatmentLevel | null
  diagnosed: Diagnosed | null
  ageDistribution: AgeDistribution | null
  asthmaInducement: AsthmaInducement | null

  hospitalStaffInfo: HospitalStaffInfo | null
  markedCardWithArea: MarkedCardWithArea | null
}

interface MarkedCardWithArea {
  aprRed: number | null
  aprYellow: number | null
  aprGreen: number | null
  yellowRate: number | null
  redRate: number | null
  greenRate: number | null
}
interface MarkedCard {
  // 打卡数
  marked: Number | null
  // 没打卡率
  other: String | null
  // 打卡率
  rate: String | null
  // 总人数
  total: Number | null
}

// 医疗数据——诊断统计
export interface DiagnosticStatistics {
  // 支气管哮喘
  bronchialAsthma: Number | null
  // 支气管哮喘？
  bronchialAsthma2: Number | null
  // 学龄前反复喘息
  recurrentWheezingInPreschool: Number | null
  // 学龄前反复喘息?
  recurrentWheezingInPreschool2: Number | null
  // 咳嗽变异性哮喘(CVA)
  coughVariantAsthma: Number | null
  // 咳嗽变异性哮喘(CVA)?
  coughVariantAsthma2: Number | null
  // 无哮喘 无哮喘总次数
  nonAsthma: Number | null
  // 支气管哮喘急性发作
  acuteAttackOfBronchialAsthma: Number | null
  // 学龄前反复喘息急性发作
  acuteOnsetOfRecurrentWheezingInPreschool: Number | null
}

// 治疗级别
interface TreatmentLevel {
  // 良好控制
  good: Number | null
  // 良好控制率
  goodRate: String | null
  // 部分控制
  part: Number | null
  // 部分控制率
  partRate: String | null
  // 未控制
  bad: Number | null
  // 未控制率
  badRate: String | null
  // // 已控制
  // controlled: Number | null
  // // 已控制率
  // controlledRate: String | null
}

// 确诊人数
interface Diagnosed {
  diagnosed: String | null
}

// 年龄分布
interface AgeDistribution {
  // 小于6岁
  lt6: Number | null
  // 大于等于6岁小于12岁
  ge12: Number | null
  // 大于等于12岁
  ge6Lt12: Number | null
}

// 哮喘诱因
export interface AsthmaInducement {
  // 感冒
  cold: Number | null
  // 剧烈运动
  strenuousExercise: Number | null
  // 大叫大笑
  yellLaugh: Number | null
  // 哭闹
  cryScream: Number | null
  // 进食甜食或冷冻食品饮料
  sweetRefrigeration: Number | null
  // 气候变化
  weather: Number | null
  // 接触动物
  animal: Number | null
  // 闻到浓烈的气味
  smell: Number | null
}

// 院内人员
interface HospitalStaff {
  // 姓名
  doctorName: String | null
  // 新增病历总数
  total: Number | null
  // 首诊病历总数
  first: Number | null
  // 复诊病历总数
  more: Number | null
  // 患者数
  patientNumber: Number | null
  // 行动计划数
  actionNumber: Number | null
  // 开方数
  presNumber: Number | null
  // 角色
  roleName: String | null
  // 医生护士id
  doctorId: String | null
}
interface HospitalStaffInfo {
  hospitalStaff: HospitalStaff[]
  total: null
}

// interface HospitalStaffInfo {
//   hospitalStaff: HospitalStaff[]
//   total: null
// }

// diagnosed 确诊人数
const state: State = {
  // 概要
  summaryList: [],
  // 总览
  overviewList: [],
  // 患者打卡率
  markedCard: { marked: null, other: null, rate: null, total: null },
  // 诊断统计
  diagnosticStatistics: null,
  treatmentLevel: null,
  diagnosed: null,
  ageDistribution: null,
  asthmaInducement: null,
  markedCardWithArea: null,

  hospitalStaffInfo: null,
}

const mutations = {
  UPDATE_SUMMARY(state: State, summaryList: SummaryList) {
    state.summaryList = summaryList
  },
  UPDATE_DATAOVERVIEW(state: State, summaryList: SummaryList) {
    state.overviewList = summaryList
  },
  UPDATE_MARKEDCARD(state: State, markedCard: MarkedCard) {
    state.markedCard = markedCard
  },
  UPDATE_BOTTOMDATA(state: State, data: Object) {
    console.log('UPDATE_BOTTOMDATA = ' + JSON.stringify(data))
    state.diagnosticStatistics = dispatchDiagnosticStatistics(data)
    state.treatmentLevel = dispathcTreatmentLevel(data)
    state.diagnosed = dispatchDiagnosed(data)
    state.ageDistribution = dispatchAgeDistribution(data)
    state.asthmaInducement = dispatchAsthmaInducement(data)
    state.markedCardWithArea = dispatchMarkedCardWithArea(data)
  },
  UPDATE_HOSPITALSTAFFINFO(state: State, hospitalStaffInfo: HospitalStaffInfo) {
    console.log(
      'UPDATE_HOSPITALSTAFFINFO =' + JSON.stringify(hospitalStaffInfo)
    )
    state.hospitalStaffInfo = hospitalStaffInfo
  },
}

const actions = {
  // 概要
  updateLocalSummary({ commit }: { commit: Commit }, data: requestType) {
    return new Promise((resolve, reject) => {
      getSummary(data)
        .then((res) => {
          console.log('updateSummary =' + JSON.stringify(res))
          const {
            first,
            more,
            patientNumber,
            submitted,
            notSubmitted,
            remoteNumber,
            actionNumber,
            actionPatients,
            pefNumber,
            breathNumber,
            asthmaNumber,
          } = res.data
          const summaryList: SummaryList = [
            { name: '新增首诊病历数', value: `${first}` },
            { name: '新增复诊病历数', value: `${more}` },
            { name: '新增患者数', value: `${patientNumber}` },
            { name: '提交病历数', value: `${submitted}` },
            { name: '未提交病历数', value: `${notSubmitted}` },
            { name: '远程复诊数', value: `${remoteNumber}` },
            { name: '行动计划制定总数', value: `${actionNumber}` },
            { name: '行动计划制定人数', value: `${actionPatients}` },
            { name: '呼气峰流速上传数', value: `${pefNumber}` },
            { name: '呼吸音采集数', value: `${breathNumber}` },
            { name: '哮喘评估控制次数', value: `${asthmaNumber}` },
          ]
          commit('UPDATE_SUMMARY', summaryList)
          resolve(summaryList)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  // 总览
  updateLocalDataOverview({ commit }: { commit: Commit }, data: requestType) {
    return new Promise((resolve, reject) => {
      getDataOverview(data)
        .then((res) => {
          console.log('updataDataOverview =' + JSON.stringify(res))
          const {
            first,
            more,
            total,
            submitted,
            notSubmitted,
            actionSubmitted,
            drugWithdrawal,
            resulted,
          } = res.data
          const summaryList: SummaryList = [
            { name: '累计首诊病历总数', value: `${first}` },
            { name: '累计复诊病历总数', value: `${more}` },
            { name: '累计病历总数', value: `${total}` },
            { name: '累计提交病历数', value: `${submitted}` },
            { name: '累计未提交病历数', value: `${notSubmitted}` },
            { name: '累计管理患者', value: `${resulted}` },
            { name: '累计行动计划制定总数', value: `${actionSubmitted}` },
            { name: '累计停药人数', value: `${drugWithdrawal}` },
          ]
          commit('UPDATE_DATAOVERVIEW', summaryList)
          resolve(summaryList)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  // 打卡率
  updateLocalMarkedRate({ commit }: { commit: Commit }, data: requestType) {
    return new Promise((resolve, reject) => {
      getMarkedRate(data)
        .then((res) => {
          console.log('updataDataOverview =' + JSON.stringify(res))
          const { marked, other, rate, total, more, visitRate } = res.data
          commit('UPDATE_MARKEDCARD', {
            marked,
            other,
            rate,
            total,
            more,
            visitRate,
          })
          resolve({ marked, other, rate, total, more, visitRate })
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  updateLocalBottomData({ commit }: { commit: Commit }, data: requestType) {
    return new Promise((resolve, reject) => {
      getBottomData(data)
        .then((res) => {
          console.log('getBottomData =' + JSON.stringify(res))
          commit('UPDATE_BOTTOMDATA', res.data)
          resolve(res.data)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  //
  updateLocalHospitalStaffs(
    { commit }: { commit: Commit },
    data: analysisType
  ) {
    return new Promise((resolve, reject) => {
      getHospitalStaffs(data)
        // 为什么不用any total就静态监测不过
        .then((res: any) => {
          console.log('getHospitalStaffs =' + JSON.stringify(res))
          commit('UPDATE_HOSPITALSTAFFINFO', {
            hospitalStaff: res.data,
            total: res.total,
          } as HospitalStaffInfo)
          resolve({ hospitalStaff: res.data, total: res.total })
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
}

// 诊断统计
function dispatchDiagnosticStatistics(data: any): DiagnosticStatistics {
  const {
    bronchialAsthma,
    bronchialAsthma2,
    recurrentWheezingInPreschool,
    recurrentWheezingInPreschool2,
    coughVariantAsthma,
    coughVariantAsthma2,
    nonAsthma,
    acuteAttackOfBronchialAsthma,
    acuteOnsetOfRecurrentWheezingInPreschool,
  } = data
  return {
    bronchialAsthma,
    bronchialAsthma2,
    recurrentWheezingInPreschool,
    recurrentWheezingInPreschool2,
    coughVariantAsthma,
    coughVariantAsthma2,
    nonAsthma,
    acuteAttackOfBronchialAsthma,
    acuteOnsetOfRecurrentWheezingInPreschool,
  }
}

// 治疗级别
function dispathcTreatmentLevel(data: any): TreatmentLevel {
  const { good, goodRate, part, partRate, bad, badRate } = data
  return { good, goodRate, part, partRate, bad, badRate }
}

// 确诊人数
function dispatchDiagnosed(data: any): Diagnosed {
  const { diagnosed } = data
  return diagnosed
}
// 年龄分布
function dispatchAgeDistribution(data: any): AgeDistribution {
  const { lt6, ge12, ge6Lt12 } = data
  return { lt6, ge12, ge6Lt12 }
}
// 哮喘诱因
function dispatchAsthmaInducement(data: any): AsthmaInducement {
  const {
    cold,
    strenuousExercise,
    yellLaugh,
    cryScream,
    sweetRefrigeration,
    weather,
    animal,
    smell,
  } = data
  return {
    cold,
    strenuousExercise,
    yellLaugh,
    cryScream,
    sweetRefrigeration,
    weather,
    animal,
    smell,
  }
}
function dispatchMarkedCardWithArea(data: any): MarkedCardWithArea {
  const { aprGreen, aprRed, aprYellow, yellowRate, redRate, greenRate } = data

  return { aprGreen, aprRed, aprYellow, yellowRate, redRate, greenRate }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
